<?php
/**
 * This is the FireFeed Registration form.
 * 
 * @uses       Zend_Form
 * @package    FireFeed
 * @subpackage Form
 */
class Form_Registration extends Zend_Form
{		 

   /**
    * init() routine called when Zend_Form objects are created.
    * It make sense to define form elements in this method.
    * 
    * @see    http://framework.zend.com/manual/en/zend.form.html
    * @return void
    */ 
	public function init()
    {
    	 $this->addSubForms(array(
    	    'user'    => new Form_UserSubForm(),
    	    'profile' => new Form_ProfileSubForm()
    	 ));
    }
    
    /**
     * Prepare a sub form for display
     *
     * @param  string|Zend_Form_SubForm $spec
     * @return Zend_Form_SubForm
     */
    public function prepareSubForm($spec)
    {
        if (is_string($spec)) {
            $subForm = $this->{$spec};
        } elseif ($spec instanceof Zend_Form_SubForm) {
            $subForm = $spec;
        } else {
            throw new Exception('Invalid argument passed to ' .
                                __FUNCTION__ . '()');
        }
        $this->setSubFormDecorators($subForm)
             ->addSubmitButton($subForm)
             ->addSubFormActions($subForm);
             
        return $subForm;
    }
    
    /**
     * Add form decorators to an individual sub form
     *
     * @param  Zend_Form_SubForm $subForm
     * @return Form_UserProfile
     */
    public function setSubFormDecorators(Zend_Form_SubForm $subForm)
    {
    	$subForm->setAttrib('id', 'userProfileForm')
    	        ->setElementFilters(array('StringTrim'));
    	        
        $subForm->setDecorators(array(
            'FormElements',
            'Form'
        ));

        $subForm->addDecorator('HtmlTag', array('tag' => '<ul>', 'class' => 'ulForm'));
        
        return $this;
    }
    
    /**
     * Add a submit button to an individual sub form
     *
     * @param  Zend_Form_SubForm $subForm
     * @return Form_UserProfile
     */
    public function addSubmitButton(Zend_Form_SubForm $subForm)
    {
    	$submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('submit')
                ->setAttrib('class', 'profileSubmit')
                ->setDecorators(array(
                    array('ViewHelper'),
                    array('HtmlTag', array('tag' => 'li', 'class' => 'element-group'))
                ));
                
        $subForm->addElement($submit);
        
        return $this;
    }
    
    /**
     * Add action and method to sub form
     *
     * @param  Zend_Form_SubForm $subForm
     * @return Form_UserProfile
     */
    public function addSubFormActions(Zend_Form_SubForm $subForm)
    {
        $subForm->setAction('/registration/process')
                ->setMethod('POST');
        return $this;
    }
}    	 